<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;

$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['diy']=$this->plugin_setting('diy');
if($_RQ['op']=='main'){
	
	exi($return);
}
/*******op*******/
if($_RQ['op']=='list'){
	//查询客户订单
	$params=array('user_id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status'] !=10){
		$params['status']=intval($_RQ['status']);
		$return['status']=$_RQ['status'];
	}else{
		$return['status']=10;
	}
	//$order_list=pdo_getall('jy_meifa_order',$params,'','','createtime desc');
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','user_id','wid','sid','money','pay','status','createtime');
	$order_list=pdo_getslice('jy_meifa_order',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	$day_start = date('Y-m-d', time());
 // $week_start = date('Y-m-d', strtotime('last day this week'));
 // $month_start = date('Y-m-01', time());
    $end = date('Y-m-d H:i:s', time());
	// 当天的零点
	$dayBegin = strtotime(date('Y-m-d', time()));
	// 当天的24
	$dayEnd = $dayBegin + 24 * 60 * 60;
	if($order_list){
		foreach($order_list as &$item){
			$user=pdo_get('core_members',array('id'=>$item['user_id'],'uniacid'=>$_SESSION['uniacid']));
			$store=pdo_get('jy_meifa_store',array('id'=>$item['sid'],'uniacid'=>$_SESSION['uniacid']));
			$worker=pdo_get('jy_meifa_worker',array('id'=>$item['wid'],'uniacid'=>$_SESSION['uniacid']));
			$item['user_name']=$user['realname'];
			$item['user_nickname']=$user['nickname'];
			$item['store_title']=$store['title'];
			$basic=$this->plugin_setting('basic');
			$item['service']=$basic['service'];
			$item['store_image']=$store['image'];
			$item['worker']=$worker;
			$num=pdo_getcolumn('jy_meifa_num',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$item['id']),'number');
			if($item['createtime']>=$$dayBegin && $item['createtime']<=$dayEnd){
				$sql = 'SELECT COUNT(1) FROM '. tablename('jy_meifa_order') . ' WHERE uniacid=:uniacid AND wid=:wid AND status=0 AND createtime <:createtime AND createtime >= UNIX_TIMESTAMP( "'.$day_start.'" ) AND createtime <= UNIX_TIMESTAMP( "'.$end.'" )';
				$paidui = pdo_fetch($sql,array(":uniacid" => $_SESSION['uniacid'],":wid" => $item['wid'],":createtime" => $item['createtime']));
				$item['paidui']=$paidui["COUNT(1)"];
			}
			else{
				pdo_update('jy_meifa_order',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'status'=>0,'id'=>$item['id']));
			}
			if($num<10){
				$item['number']=$worker['qz'].'000'.$num;
			}
			
			elseif($num>9 && $num<100){
				$item['number']=$worker['qz'].'00'.$num;
				}
			else{
				$item['number']=$worker['qz'].'0'.$num;
				}
			
		$item['createtime']=date('Y-m-d H:i:s', $item['createtime']);
	}
	
	}
	
	$return['order_list']=$order_list;
	$return['status']=$status;
	exi($return);
}

if($_RQ['op']=='receive'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
}
if($_RQ['op']=='cancel'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_update('jy_meifa_order',array('status'=>3),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('取消订单成功');
}
if($_RQ['op']=='comment'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$iscomment=pdo_get('jy_meifa_comment',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$_RQ['id']));
	if($iscomment){
		exi('您已经评论过该订单','error');
	}
	$postdata=get_postdata();
	//print_r($postdata);exit;
	$order=pdo_get('jy_meifa_order',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	if($postdata){
		$insert=array(
		'order_id'=>$_RQ['id'],
		'uniacid'=>$_SESSION['uniacid'],
		'user_id'=>$_SESSION['uid'],
		'sid'=>$order['sid'],
		'wid'=>$order['wid'],
		'comment'=>$postdata['comment'],
		'image'=>$postdata['image'],
		'images'=>ijson_encode($postdata['images']),
		'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_meifa_comment',$insert);
	
	
	}
	
	exi('评价成功','','jy_meifa/order/list/status=2');
	
}
if($_RQ['op']=='old'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	$isold=pdo_get('jy_meifa_log',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$_RQ['id']));
	if($iscomment){
		exi('您已经上传历史服务','error');
	}
	$postdata=get_postdata();
	//print_r($postdata);exit;
	if($postdata){
		$insert=array(
		'order_id'=>$_RQ['id'],
		'uniacid'=>$_SESSION['uniacid'],
		'user_id'=>$_SESSION['uid'],
		'remark'=>trim($postdata['msg']),
		'images'=>ijson_encode($postdata['images']),
		'createtime'=>TIMESTAMP,
		);
		pdo_insert('jy_meifa_log',$insert);
	
	
	}
	
	exi('上传成功','','jy_meifa/order/list/status=2');
	
}
if($_RQ['op']=='del'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	pdo_delete('jy_meifa_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	exi('删除订单成功');
}
if($_RQ['op']=='pay'){
	if(!$_RQ['id']){
		exi('订单不存在，请重新选择订单','error');
	}
	
	$order=pdo_get('jy_meifa_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	$title=pdo_getcolumn('jy_meifa_store',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['sid']),'title');
	$basic=$this->plugin_setting('basic');
	$service=$basic['service'];
	$params['fee']=$order['money'];
	$params['title']=$title.'--'.$service;
	$params['id']=$_RQ['id'];
	$params['plugin']='jy_meifa';
	$params['back_url']='jy_meifa/order/payresult';
	cfc('core')->pay($params);
}
if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['payresult'];unset($_SESSION['payresult']);
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error','list');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error','list');
	}
	$order=pdo_get('jy_meifa_order',array('id'=>$payresult['id'],'uniacid'=>$_SESSION['uniacid']));
	if(!$order){
		exi('支付失败！指定订单不存在！','error');
	}
	if($order['money']!=$payresult['fee']){
		exi('支付失败！订单金额不匹配！','error');
	}
	pdo_update('jy_meifa_order',array('status'=>2,'pay'=>$payresult['fee']),array('id'=>$order['id']));
	//确认订单后，计算员工佣金,存入佣金日志表，并更新员工表员工总佣金
	$worker_id=pdo_getcolumn('jy_meifa_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']),'wid');
	$pay=pdo_getcolumn('jy_meifa_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$order['id']),'pay');
	$lev_id=pdo_getcolumn('jy_meifa_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'lev_id');
	$rate=pdo_getcolumn('jy_meifa_lev',array('uniacid'=>$_SESSION['uniacid'],'id'=>$lev_id),'rate');
	//print_r($pay.'dd');exit;
	if($rate>0){
		$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'worker_id'=>$worker_id,
		'order_id'=>$order['id'],
		'money'=>$pay*$rate/100,
		'createtime'=>TIMESTAMP,
		);
		//exi($rate,'error');
		pdo_insert('jy_meifa_commission_log',$insert);
		$commission=pdo_getcolumn('jy_meifa_worker',array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id),'commission');
		$commission=$commission+$pay*$rate/100;
		pdo_update('jy_meifa_worker',array('commission'=>$commission),array('uniacid'=>$_SESSION['uniacid'],'id'=>$worker_id));
	
}
exi('支付成功','success','jy_meifa/order/list/status=2');
}